﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;

    public class LoadDetailDA : ILoadDetailDA
    {
        private string CONSIGNEE_ID = "@CONSIGNEE_ID";
        private string CONSIGNEE_NAME = "@CONSIGNEE_NAME";
        private string CREATED_BY = "@CREATED_BY";
        private string EXTERNAL_ORDER_ID = "@EXTERNAL_ORDER_ID";
        private string EXTERNAL_SHIPMENT_ID = "@EXTERNAL_SHIPMENT_ID";
        private string FLOW_ORDER_ID = "@FLOW_ORDER_ID";
        private string LINE_ID = "@LINE_ID";
        private string LOAD_ID = "@LOAD_ID";
        private string OH_TYPE = "@OH_TYPE";
        private string ORDER_ID = "@ORDER_ID";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_LOAD_DETAIL_ID_ORDER_ID_UNIQUE = "SELECT COUNT(1) FROM @_@LOAD_DETAIL WHERE LOAD_ID = @LOAD_ID AND ORDER_ID = @ORDER_ID ";
        private string SQL_CHECK_LOAD_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@LOAD_DETAIL WHERE LOAD_ID = @LOAD_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_LOAD_DETAIL = " DELETE FROM @_@LOAD_DETAIL WHERE LOAD_ID = @LOAD_ID ";
        private string SQL_DELETE_LOAD_DETAIL_BY_LINE_ID = " DELETE FROM @_@LOAD_DETAIL WHERE LOAD_ID = @LOAD_ID AND LINE_ID = @LINE_ID";
        private string SQL_INSERT_LOAD_DETAIL = " INSERT INTO @_@LOAD_DETAIL ( WH_ID, LOAD_ID, LINE_ID, STATUS, ORDER_ID, EXTERNAL_SHIPMENT_ID, EXTERNAL_ORDER_ID, TOTAL_UNITS, TOTAL_CUBE, TOTAL_WEIGHT, TOTAL_COST, TOTAL_VALUE, OH_TYPE, FLOW_ORDER_ID, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @LOAD_ID, @LINE_ID, @STATUS, @ORDER_ID, @EXTERNAL_SHIPMENT_ID, @EXTERNAL_ORDER_ID, @TOTAL_UNITS, @TOTAL_CUBE, @TOTAL_WEIGHT, @TOTAL_COST, @TOTAL_VALUE, @OH_TYPE, @FLOW_ORDER_ID, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_LOAD_DETAIL = " SELECT  LD.ROWID, LD.WH_ID, LD.LOAD_ID, LD.LINE_ID, O.STATUS, LD.ORDER_ID, LD.EXTERNAL_SHIPMENT_ID, LD.EXTERNAL_ORDER_ID, LD.TOTAL_UNITS,  LD.TOTAL_CUBE, LD.TOTAL_WEIGHT, LD.TOTAL_COST, LD.TOTAL_VALUE, LD.OH_TYPE, LD.FLOW_ORDER_ID, LD.CREATED_BY, LD.CREATED_DATE,  LD.UPDATED_BY, LD.UPDATED_DATE,O.CONSIGNEE_ID,C.COMPANY AS CONSIGNEE_NAME,O.REMARK ,O.ROUTE  FROM  @_@LOAD_DETAIL LD LEFT JOIN @_@ORDERS O ON O.ORDER_ID = LD.ORDER_ID LEFT JOIN @_@CUSTOMER C ON C.CUSTOMER_ID = O.CONSIGNEE_ID ";
        private string SQL_UPDATE_LOAD_DETAIL = " UPDATE @_@LOAD_DETAIL SET WH_ID = @WH_ID, STATUS = @STATUS, ORDER_ID = @ORDER_ID, EXTERNAL_SHIPMENT_ID = @EXTERNAL_SHIPMENT_ID, EXTERNAL_ORDER_ID = @EXTERNAL_ORDER_ID, TOTAL_UNITS = @TOTAL_UNITS, TOTAL_CUBE = @TOTAL_CUBE, TOTAL_WEIGHT = @TOTAL_WEIGHT, TOTAL_COST = @TOTAL_COST, TOTAL_VALUE = @TOTAL_VALUE, OH_TYPE = @OH_TYPE, FLOW_ORDER_ID = @FLOW_ORDER_ID, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE LOAD_ID = @LOAD_ID AND LINE_ID = @LINE_ID  ";
        private string STATUS = "@STATUS";
        private string TOTAL_COST = "@TOTAL_COST";
        private string TOTAL_CUBE = "@TOTAL_CUBE";
        private string TOTAL_UNITS = "@TOTAL_UNITS";
        private string TOTAL_VALUE = "@TOTAL_VALUE";
        private string TOTAL_WEIGHT = "@TOTAL_WEIGHT";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckLoadDetailIDOrderIDUnique(string loadDetailID, string orderID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loadDetailID), new SqlParameter("@ORDER_ID", orderID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_LOAD_DETAIL_ID_ORDER_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckLoadDetailIDUnique(string loadDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loadDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_LOAD_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckLoadDetailIDUnique(DataBase dataBase, DbTransaction tran, string loadDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loadDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_LOAD_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteLoadDetail(List<string> loadDetailID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < loadDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loadDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOAD_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteLoadDetail(DataBase dataBase, DbTransaction tran, List<string> loadDetailID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < loadDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loadDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOAD_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteLoadDetailByLineID(string loadDetailID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < lineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loadDetailID), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOAD_DETAIL_BY_LINE_ID.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteLoadDetailByLineID(DataBase dataBase, DbTransaction tran, string loadDetailID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < lineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loadDetailID), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOAD_DETAIL_BY_LINE_ID.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllLoadDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOAD_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllLoadDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOAD_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<LoadDetailInfo> GetLoadDetailByID(string loaddetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOAD_DETAIL.Replace("@_@", whLoginID) + " WHERE LD.LOAD_ID = @LOAD_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loaddetailID) };
            List<LoadDetailInfo> loadDetailInfoList = new List<LoadDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    LoadDetailInfo loadDetailInfo = new LoadDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOAD_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["STATUS"].ToString(), reader["ORDER_ID"].ToString(), reader["EXTERNAL_SHIPMENT_ID"].ToString(), reader["EXTERNAL_ORDER_ID"].ToString(), (reader["TOTAL_UNITS"].ToString() != "") ? decimal.Parse(reader["TOTAL_UNITS"].ToString()) : 0M, (reader["TOTAL_CUBE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_CUBE"].ToString())) : ((double) 0f), (reader["TOTAL_WEIGHT"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_WEIGHT"].ToString())) : ((double) 0f), (reader["TOTAL_COST"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_COST"].ToString())) : ((double) 0f), (reader["TOTAL_VALUE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_VALUE"].ToString())) : ((double) 0f), reader["OH_TYPE"].ToString(), reader["FLOW_ORDER_ID"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["CONSIGNEE_ID"].ToString(), reader["CONSIGNEE_NAME"].ToString(), reader["REMARK"].ToString(), reader["ROUTE"].ToString());
                    loadDetailInfoList.Add(loadDetailInfo);
                }
            }
            return loadDetailInfoList;
        }

        public List<LoadDetailInfo> GetLoadDetailByID(DataBase dataBase, DbTransaction tran, string loaddetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOAD_DETAIL.Replace("@_@", whLoginID) + " WHERE LD.LOAD_ID = @LOAD_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loaddetailID) };
            List<LoadDetailInfo> loadDetailInfoList = new List<LoadDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                LoadDetailInfo loadDetailInfo = new LoadDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOAD_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["STATUS"].ToString(), reader["ORDER_ID"].ToString(), reader["EXTERNAL_SHIPMENT_ID"].ToString(), reader["EXTERNAL_ORDER_ID"].ToString(), (reader["TOTAL_UNITS"].ToString() != "") ? decimal.Parse(reader["TOTAL_UNITS"].ToString()) : 0M, (reader["TOTAL_CUBE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_CUBE"].ToString())) : ((double) 0f), (reader["TOTAL_WEIGHT"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_WEIGHT"].ToString())) : ((double) 0f), (reader["TOTAL_COST"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_COST"].ToString())) : ((double) 0f), (reader["TOTAL_VALUE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_VALUE"].ToString())) : ((double) 0f), reader["OH_TYPE"].ToString(), reader["FLOW_ORDER_ID"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["CONSIGNEE_ID"].ToString(), reader["CONSIGNEE_NAME"].ToString(), reader["REMARK"].ToString(), reader["ROUTE"].ToString());
                loadDetailInfoList.Add(loadDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return loadDetailInfoList;
        }

        public LoadDetailInfo GetLoadDetailByIDLineID(string loaddetailID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOAD_DETAIL.Replace("@_@", whLoginID) + " WHERE LD.LOAD_ID = @LOAD_ID AND LD.LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loaddetailID), new SqlParameter("@LINE_ID", lineID) };
            LoadDetailInfo loadDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    loadDetailInfo = new LoadDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOAD_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["STATUS"].ToString(), reader["ORDER_ID"].ToString(), reader["EXTERNAL_SHIPMENT_ID"].ToString(), reader["EXTERNAL_ORDER_ID"].ToString(), (reader["TOTAL_UNITS"].ToString() != "") ? decimal.Parse(reader["TOTAL_UNITS"].ToString()) : 0M, (reader["TOTAL_CUBE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_CUBE"].ToString())) : ((double) 0f), (reader["TOTAL_WEIGHT"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_WEIGHT"].ToString())) : ((double) 0f), (reader["TOTAL_COST"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_COST"].ToString())) : ((double) 0f), (reader["TOTAL_VALUE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_VALUE"].ToString())) : ((double) 0f), reader["OH_TYPE"].ToString(), reader["FLOW_ORDER_ID"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["CONSIGNEE_ID"].ToString(), reader["CONSIGNEE_NAME"].ToString(), reader["REMARK"].ToString(), reader["ROUTE"].ToString());
                }
            }
            return loadDetailInfo;
        }

        public LoadDetailInfo GetLoadDetailByIDLineID(DataBase dataBase, DbTransaction tran, string loaddetailID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOAD_DETAIL.Replace("@_@", whLoginID) + " WHERE LD.LOAD_ID = @LOAD_ID AND LD.LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOAD_ID", loaddetailID), new SqlParameter("@LINE_ID", lineID) };
            LoadDetailInfo loadDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                loadDetailInfo = new LoadDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOAD_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["STATUS"].ToString(), reader["ORDER_ID"].ToString(), reader["EXTERNAL_SHIPMENT_ID"].ToString(), reader["EXTERNAL_ORDER_ID"].ToString(), (reader["TOTAL_UNITS"].ToString() != "") ? decimal.Parse(reader["TOTAL_UNITS"].ToString()) : 0M, (reader["TOTAL_CUBE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_CUBE"].ToString())) : ((double) 0f), (reader["TOTAL_WEIGHT"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_WEIGHT"].ToString())) : ((double) 0f), (reader["TOTAL_COST"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_COST"].ToString())) : ((double) 0f), (reader["TOTAL_VALUE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_VALUE"].ToString())) : ((double) 0f), reader["OH_TYPE"].ToString(), reader["FLOW_ORDER_ID"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["CONSIGNEE_ID"].ToString(), reader["CONSIGNEE_NAME"].ToString(), reader["REMARK"].ToString(), reader["ROUTE"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return loadDetailInfo;
        }

        public LoadDetailInfo GetLoadDetailInfoByOrderID(string orderID, string whLoginID)
        {
            string sql = " SELECT O.ORDER_ID,O.EXTERN_ORDER_ID,O.CONSIGNEE_ID,C.COMPANY AS CONSIGNEE_NAME,O.REMARK,O.ROUTE,SUM(OD.ALLOCATED_QTY+OD.PICKED_QTY) AS TOTAL_UNITS,SUM(OD.[CUBE]*(OD.ALLOCATED_QTY+OD.PICKED_QTY)) AS TOTAL_CUBE,SUM(OD.GROSS_WGT*(OD.ALLOCATED_QTY+OD.PICKED_QTY)) AS TOTAL_WEIGHT  FROM @_@ORDERS O LEFT JOIN @_@ORDER_DETAIL OD ON OD.ORDER_ID = O.ORDER_ID LEFT JOIN @_@CUSTOMER C ON C.CUSTOMER_ID = O.CONSIGNEE_ID  WHERE O.ORDER_ID = @ORDER_ID GROUP BY O.ORDER_ID,O.EXTERN_ORDER_ID,O.CONSIGNEE_ID,C.COMPANY,O.REMARK,O.ROUTE ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID) };
            LoadDetailInfo loadDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql.Replace("@_@", whLoginID), paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003 = new DateTime();
                    loadDetailInfo = new LoadDetailInfo(0, "", "", 0, "", reader["ORDER_ID"].ToString(), "", reader["EXTERN_ORDER_ID"].ToString(), (reader["TOTAL_UNITS"].ToString() != "") ? decimal.Parse(reader["TOTAL_UNITS"].ToString()) : 0M, (reader["TOTAL_CUBE"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_CUBE"].ToString())) : ((double) 0f), (reader["TOTAL_WEIGHT"].ToString() != "") ? ((double) float.Parse(reader["TOTAL_WEIGHT"].ToString())) : ((double) 0f), 0.0, 0.0, "", "", "", CS00003, "", new DateTime(), reader["CONSIGNEE_ID"].ToString(), reader["CONSIGNEE_NAME"].ToString(), reader["REMARK"].ToString(), reader["ROUTE"].ToString());
                }
            }
            return loadDetailInfo;
        }

        public int InsertLoadDetail(LoadDetailInfo loadDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                loadDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_LoadDetail_Parameters(loadDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOAD_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertLoadDetail(DataBase dataBase, DbTransaction tran, LoadDetailInfo loadDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                loadDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_LoadDetail_Parameters(loadDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOAD_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_LoadDetail_Parameters(LoadDetailInfo loadDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.LOAD_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20), new SqlParameter(this.ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.EXTERNAL_SHIPMENT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.EXTERNAL_ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.TOTAL_UNITS, SqlDbType.Decimal, 13), new SqlParameter(this.TOTAL_CUBE, SqlDbType.Float, 8), new SqlParameter(this.TOTAL_WEIGHT, SqlDbType.Float, 8), new SqlParameter(this.TOTAL_COST, SqlDbType.Float, 8), new SqlParameter(this.TOTAL_VALUE, SqlDbType.Float, 8), new SqlParameter(this.OH_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.FLOW_ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(loadDetailInfo.WhID))
            {
                paramArray[0].Value = loadDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = loadDetailInfo.LoadID;
            paramArray[2].Value = loadDetailInfo.LineID;
            paramArray[3].Value = loadDetailInfo.Status;
            if (!string.IsNullOrEmpty(loadDetailInfo.OrderID))
            {
                paramArray[4].Value = loadDetailInfo.OrderID;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(loadDetailInfo.ExternalShipmentID))
            {
                paramArray[5].Value = loadDetailInfo.ExternalShipmentID;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(loadDetailInfo.ExternalOrderID))
            {
                paramArray[6].Value = loadDetailInfo.ExternalOrderID;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            paramArray[7].Value = loadDetailInfo.TotalUnits;
            paramArray[8].Value = loadDetailInfo.TotalCube;
            paramArray[9].Value = loadDetailInfo.TotalWeight;
            paramArray[10].Value = loadDetailInfo.TotalCost;
            paramArray[11].Value = loadDetailInfo.TotalValue;
            if (!string.IsNullOrEmpty(loadDetailInfo.OhType))
            {
                paramArray[12].Value = loadDetailInfo.OhType;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(loadDetailInfo.FlowOrderID))
            {
                paramArray[13].Value = loadDetailInfo.FlowOrderID;
            }
            else
            {
                paramArray[13].Value = DBNull.Value;
            }
            paramArray[14].Value = loadDetailInfo.CreatedBy;
            paramArray[15].Value = loadDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateLoadDetail(LoadDetailInfo loadDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                loadDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_LoadDetail_Parameters(loadDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOAD_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateLoadDetail(DataBase dataBase, DbTransaction tran, LoadDetailInfo loadDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                loadDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_LoadDetail_Parameters(loadDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOAD_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public void UpdateLoadDetailStatusByOrderID(string orderID, string status, string updatedBy, string whLoginID)
        {
            string sql = "UPDATE @_@LOAD_DETAIL SET STATUS = @STATUS,UPDATED_BY = @UPDATED_BY,UPDATED_DATE = GETDATE() WHERE ORDER_ID = @ORDER_ID";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATUS", status), new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@UPDATED_BY", updatedBy) };
            DBHelper.ExecuteNonQuery(CommandType.Text, sql.Replace("@_@", whLoginID), paras);
        }

        public void UpdateLoadDetailStatusByOrderID(DataBase dataBase, DbTransaction tran, string orderID, string status, string updatedBy, string whLoginID)
        {
            string sql = "UPDATE @_@LOAD_DETAIL SET STATUS = @STATUS,UPDATED_BY = @UPDATED_BY,UPDATED_DATE = GETDATE() WHERE ORDER_ID = @ORDER_ID";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATUS", status), new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@UPDATED_BY", updatedBy) };
            dataBase.ExecuteNonQuery(tran, CommandType.Text, sql.Replace("@_@", whLoginID), paras);
        }
    }
}

